package zhuzhuwang;

import hb.House;
import hb.HouseDao;
import hb.User;
import hb.UserDao;
import hb.RentOrder;
import hb.RentOrderDao;
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HouseController extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession(true);
		String address="/index.jsp";
		String action=(request.getParameter("action")).trim();
		System.out.println(action);
		
		if(action.equals("confirmHouse")){
			String houseID=(request.getParameter("houseID")).trim();
			House temp=(new HouseDao()).getHouse(houseID);
			temp.setStatus(0);
			(new HouseDao()).update(temp);
			
			List orderList=(new RentOrderDao()).getAll();
			session.setAttribute("orderList", orderList);
			List houseList=(new HouseDao()).getAll();
			session.setAttribute("houseList", houseList);
			
			address="/order.jsp";
		}
		
		if(action.equals("deleteHouse")){
			String houseID=(request.getParameter("houseID")).trim();
			House temp=(new HouseDao()).getHouse(houseID);
			(new HouseDao()).delete(temp);
			
			List orderList=(new RentOrderDao()).getAll();
			session.setAttribute("orderList", orderList);
			List houseList=(new HouseDao()).getAll();
			session.setAttribute("houseList", houseList);
			
			address="/order.jsp";
		}
		
		RequestDispatcher dispatcher=request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
